﻿using System;
using System.Collections.Generic;
using System.Linq;
using Hopscotch.Common.Contracts.AppService;
using Hopscotch.Domain.Enquiry;
using Hopscotch.Server.Common;
using Hopscotch.Server.Common.Contracts;

namespace Hopscotch.Server.EnqServiceFake
{
    public class TradeEnqService : FakeEnqServiceIntKey<TradeEnq>, IInternalTradeEnqService
    {
        private static bool isInitialized;
        private readonly ITradeService tradeService;

        public TradeEnqService(ITradeService tradeService, TradeEnricher tradeEnricher)
        {
            this.tradeService = tradeService;
            Initialize(tradeEnricher);
        }

        private void Initialize(TradeEnricher tradeEnricher)
        {
            if (isInitialized)
                return;

            // Quickly nip through the Trade App service and build some fake enquiry Trades
            const int TradeIdStart = 1000;
            const int TotalTestTrades = 8;
            for (int i = 0; i < TotalTestTrades; i++)
            {
                var tradeId = TradeIdStart + i;
                var trade = tradeService.Get(tradeId);

                var tradeEnq = tradeEnricher.Enrich(trade);

                Add(tradeEnq);
            }

            isInitialized = true;
        }

        public List<TradeEnq> Get(DateTime tradeDate)
        {
            var qry = from trade in FakeData.Values
                      where trade.TradeDt == tradeDate
                      select trade;

            return qry.ToList();
        }
    }
}
